<template>
  <!-- 患者信息 -->
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:user:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
        >导出模版
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="患者身份证号" align="center" key="idcard" prop="idcard"/>
      <el-table-column label="身份类型" align="center" key="identityType" prop="identityType"/>
      <el-table-column label="分值" align="center" key="score" prop="score"/>
      <el-table-column label="记录日期" align="center" key="recordDate" prop="recordDate"/>
      <el-table-column label="登记人" align="center" key="recorder" prop="recorder"/>
      <el-table-column label="有效期" align="center" key="validitydate" prop="validitydate"/>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.userId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="searchScore(scope.row)"


          >信用查询
          </el-button> -->

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入身份证号" maxlength="30"/>
        </el-form-item>
        <el-form-item label="身份类型" prop="identityType">
          <el-input v-model="form.identityType" placeholder="请输入身份类型" maxlength="30"/>
        </el-form-item>
        <el-form-item label="登记人" prop="recorder">
          <el-input v-model="form.recorder" placeholder="请输入登记人" maxlength="30"/>
        </el-form-item>
        <el-form-item label="分值" prop="score">
          <el-input v-model="form.score" placeholder="请输入分值" maxlength="30"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 信用查询对话框 -->
    <el-dialog title="信用查询" :visible.sync="scoreInfo.open" width="80%" append-to-body>
      <div class="creditBox">
        <div class="leftBox">
          <div class="chart-wrapper">
            <pie-chart ref="pieEl"/>
          </div>
          <div class="bigNum"><p>{{ userInfo.finalScore }}</p>最终信用评分</div>
        </div>
        <div class="rightBox">
          <div class="panel" style="height: 150px">
            <div class="headerBox"><img src="../../../assets/images/icon1.png"/></div>
            <div class="name">
              <div class="line" style="font-size: 18px">患者身份证号：{{ userInfo.idcard }}</div>
              <div class="line"

              >
                <p>查询人：{{ userInfo.queriedBy }}</p>
                <p>查询时间：{{ userInfo.queryTime }}</p>

              </div>
            </div>
          </div>
          <div class="panel" style="height: 310px;margin-top: 18px">
            <div class="panelBox">
              <div class="line">明细类型：{{ userInfo.detailType }}</div>
              <div class="line">明细描述：{{ userInfo.detailDescription }}</div>
              <div class="line">明细评分：{{ userInfo.detailScore }}</div>
            </div>


          </div>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { searchList, exportUser, delUser, addUser, updateUser, importTemplate } from '@/api/credit/patient'
import { getToken } from '@/utils/auth'
import { searchCredit } from '@/api/credit/creditTable'
import PieChart from '../creditTable/PieChart'

export default {
  components: {

    PieChart

  },
  name: 'patient',
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/credit/importData'
      },
      //信用查询
      scoreInfo: { open: false },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idcard: ''
      },

      // 表单校验
      rules: {
        recorder: [
          { required: true, message: '登记人不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '登记人名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        idcard: [
          { required: true, message: '证件号不能为空', trigger: 'blur' }
        ],
        identityType: [
          { required: true, message: '身份类型不能为空', trigger: 'blur' }
        ],
        score: [
          { required: true, message: '分值不能为空', trigger: 'blur' }
        ],
      },

      userInfo: {
        'idcard': null,
        'queryTime': null,
        'queriedBy': null,
        'insuranceScore': null,
        'identityScore': null,
        'recordScore': null,
        'staffScore': null,
        'blacklistScore': null,
        'finalScore': null,
        'detailType': null,
        'detailDescription': null,
        'detailScore': null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      searchList(this.queryParams).then(response => {
          this.userList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 导出模版按钮操作 */
    handleExport() {

      exportUser().then(response => {
          console.log('response', response)
          // window.location.href = this.apiUrl + response.msg
          this.$download.name(response.msg)
        }
      )
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '用户导入'
      this.upload.open = true
    },
    // 提交导入上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        idcard: undefined,
        identityType: undefined,
        recorder: undefined,
        score:undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置搜索条件按钮操作 */
    resetQuery() {

      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.idcard = row.idcard
      this.form.identityType = row.identityType
      this.form.recorder = row.recorder
      this.form.score = row.score
      this.form.id = row.id
      this.open = true
      this.title = '修改用户'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids
      console.log('userIds', userIds)
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser({ ids: userIds.toString() })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },

    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.$download.name(response.msg)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },

    //信用查询
    searchScore(row) {

      console.log('row', row)
      this.loading = true
      //'12348'
      searchCredit({ idcard: row.idcard }).then(response => {
          this.loading = false
          if (!response.data) {
            this.$modal.msg(response.msg)
            return

          }
          this.scoreInfo.open = true
          this.userInfo = {
            'idcard': response.data.idcard,
            'queryTime': response.data.queryTime,
            'queriedBy': response.data.queriedBy,
            'insuranceScore': response.data.insuranceScore ? response.data.insuranceScore : 0,//医保信息评分
            'identityScore': response.data.identityScore ? response.data.identityScore : 0,//患者身份评分
            'recordScore': response.data.recordScore ? response.data.recordScore : 0,//就诊记录评分
            'staffScore': response.data.staffScore ? response.data.staffScore : 0,//医护人员主观评分
            'blacklistScore': response.data.blacklistScore ? response.data.blacklistScore : 0,//黑名单评分
            'finalScore': response.data.finalScore,
            'detailType': response.data.detailType,
            'detailDescription': response.data.detailDescription,
            'detailScore': response.data.detailScore
          }
          setTimeout(() => {
            this.$refs.pieEl.initChart([
                { value: this.userInfo.insuranceScore, name: '医保信息评分' },
                { value: this.userInfo.identityScore, name: '患者身份评分' },
                { value: this.userInfo.recordScore, name: '就诊记录评分' },
                { value: this.userInfo.staffScore, name: '医护人员主观评分' },
                { value: this.userInfo.blacklistScore, name: '黑名单评分' }
              ]
            )
          }, 200)

          this.loading = false
        }
      )
    }

  }
}
</script>
<style scoped>
.creditBox {
  display: flex;
  justify-content: space-around; /* 水平方向间隔分布 */
  gap: 20px; /* 设置间隔为20px */
  height: 480px;
  padding-bottom: 50px;
  box-sizing: content-box;
}

.leftBox {
  height: 100%;
  flex: 3;
  background: #e7effc;
  border: solid 1px #afceff;
  /*box-shadow: 0 0 5px #eee;*/
}

.rightBox {
  height: 100%;
  flex: 4;

  /*box-shadow: 0 0 5px #eee;*/
}

.chart-wrapper {
  padding: 15px 15px 0;
  margin-bottom: 20px;
}

.bigNum {
  font-size: 18px;
  text-align: center;
  color: #333;
  margin-top: -20px;
}

.bigNum p {
  font-weight: 600;
  font-size: 52px;
  color: #5c78ff;
}

.panel {
  position: relative;
  background: #e7effc;
  border: solid 1px #afceff;
  width: 100%;
}

.headerBox {
  width: 110px;
  height: 110px;
  margin: 20px 0 0 30px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8);
  border: solid 1px #afceff;
  padding: 10px;
  box-sizing: border-box;
}

.headerBox img {
  width: 100%;
  height: 100%;
}

.name {
  position: absolute;
  top: 30px;
  left: 160px;

  width: calc(100% - 200px);


}

.line {
  display: flex;
  justify-content: space-between;
  width: 100%;
  font-size: 16px;
  line-height: 40px;
  color: #333;

}

.panelBox {
  padding: 20px;
}

.panelBox .line {
  margin-bottom: 10px;
}
</style>
